package com.surface.hao.data;

import com.surface.hao.pojo.Content;
import com.surface.hao.util.SystemUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;


/**
 * 显示层用到的数据,该数据主要影响界面
 * @author songhao
 * @CreateDate 2015-04-09
 */
public class DataModel {

    /**
     * 当前表格的所有数据
     */
    public static Object[][] allTableData = null;

    /**
     * 选中的所有数据
     */
    public static Object[][] selectedData = null;

    //---------------对allTableData和selectedData的操作 外部调用-------------
    /**
     * 设置全部表格内容
     */
    public static void setAllTableData(List<Content> contentList){
        clearAllTableData();
        allTableData = initObjectArrByModelList(contentList);
    }

    /**
     * 设置选中部分表格内容
     * @param contentList
     */
    public static void setSelectedData(List<Content> contentList){
        selectedData = initObjectArrByModelList(contentList);
    }

    /**
     * 添加一个选中的表格
     * @param obj
     */
    public static void addSelectedData(Object[] obj){
        if(selectedData == null){
            List<Content> contentList = new ArrayList<Content>();
            Content content = objectToBean(obj);
            contentList.add(content);
            setSelectedData(contentList);
        }else{
            selectedData = addObjectArrByObject(selectedData,obj);
        }
        System.out.println("当前选中了:" + selectedData.length);
    }

    /**
     * 取消添加一个选中的表格
     * @param obj
     */
    public static void removeSelectedData(Object[] obj) {
        selectedData = removeObjectArrByObject(selectedData,obj);
        System.out.println("当前选中了:" + selectedData.length);
    }

    /**
     * 删除一个选中的表格
     */
    public static void delSelectedData(Object[] obj){

    }

    /**
     * Object和bean之间的相互转换
     * @param object
     * @return
     */
    public static Content objectToBean(Object[] object){
        Content content = new Content();
        ImageIcon icon = (ImageIcon)object[1];
        String path = icon.getDescription();
        File file = new File(path);
        String name = file.getName();
        if(name.contains("file")){
            content.setIsFile((byte)1);
        }else{
            content.setIsFile((byte)0);
        }
        content.setId((Integer) object[2]);
        content.setCname((String) object[3]);
        content.setSize((Integer) object[4]);
        content.setCreateTime((String) object[5]);
        return content;
    }

    public static void clearAllTableData(){
        allTableData = null;
    }

    public static void clearSelectedData(){
        selectedData = null;
    }

    //------------------对Object[][]数据进行增 删 改 查 转换内部使用--------------

    /**
     * 通过一个List集合对二维数组进行初始化
     * @param contentList
     */
    public static Object[][] initObjectArrByModelList(List<Content> contentList){
        Object[][] data = null;
        if(contentList == null || contentList.size() == 0){
            return data;
        }
        int size = contentList.size();
        //将contentList转换成二位数组
        data = new Object[size][6];
        for(int i = 0; i < size; i++){
            Content content = contentList.get(i);
            data[i][0] = false;
            String path = content.getIsFile() == 0 ? "dir.jpg" : "file.jpg";
            data[i][1] = new ImageIcon(SystemUtils.getImagePath(path));
            data[i][2] = content.getId();
            data[i][3] = content.getCname();
            data[i][4] = content.getSize();
            data[i][5] = content.getCreateTime();
        }
        return data;
    }

    public static Object[][] addObjectArrByObject(Object[][] dataArr,Object[] data){
        if(dataArr == null){
            return dataArr;
        }
        List<Content> contentList = objectArrToList(dataArr);
        contentList.add(objectToBean(data));
        return initObjectArrByModelList(contentList);
    }

    private static Object[][] removeObjectArrByObject(Object[][] dataArr,
                                                      Object[] obj) {
        if(dataArr == null){
            return dataArr;
        }
        List<Content> contentList = objectArrToList(dataArr);
        List<Content> newCList = new ArrayList<Content>();
        for(Content content : contentList){
            int id = content.getId();
            if(id != (Integer)obj[2]){
                newCList.add(content);
            }
        }
        return initObjectArrByModelList(newCList);
    }

    public static void clearDataTable(Object[][] dataArr){
        dataArr = null;
    }

    /**
     * List和数组之间的想换转换
     * @param dataArr
     * @return
     */
    public static List<Content> objectArrToList(Object[][] dataArr){
        List<Content> contentList = null;
        if(dataArr == null || dataArr.length == 0){
            return contentList;
        }
        contentList = new ArrayList<Content>();
        int size = dataArr.length;
        for(int i = 0; i < size; i++){
            Object[] object = dataArr[i];
            contentList.add(objectToBean(object));
        }
        return contentList;
    }
}
